home *** CD-ROM | disk | FTP | other *** search
/ Young Minds / Young Minds Interactive CD-ROM.ISO / gnuchess / gnuchess.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-04-30  |  62.8 KB  |  2,298 lines

  1. /*
  2.   C source for CHESS  
  3.  
  4.   Revision: 4-25-88
  5.  
  6.   Copyright (C) 1986, 1987, 1988 Free Software Foundation, Inc.
  7.   Copyright (c) 1988   John Stanback
  8.  
  9.   This file is part of CHESS.
  10.  
  11.   CHESS is distributed in the hope that it will be useful,
  12.   but WITHOUT ANY WARRANTY.  No author or distributor
  13.   accepts responsibility to anyone for the consequences of using it
  14.   or for whether it serves any particular purpose or works at all,
  15.   unless he says so in writing.  Refer to the CHESS General Public
  16.   License for full details.
  17.  
  18.   Everyone is granted permission to copy, modify and redistribute
  19.   CHESS, but only under the conditions described in the
  20.   CHESS General Public License.   A copy of this license is
  21.   supposed to have been given to you along with CHESS so you
  22.   can know your rights and responsibilities.  It should be in a
  23.   file named COPYING.  Among other things, the copyright notice
  24.   and this notice must be preserved on all copies.
  25. */
  26.  
  27.  
  28. #include <stdio.h>
  29. #include <ctype.h>
  30.  
  31. #ifdef MSDOS
  32. #include <stdlib.h>
  33. #include <time.h>
  34. #include <alloc.h>
  35. #define ttblsz 4096
  36. #else
  37. #include <sys/param.h>
  38. #include <sys/times.h>
  39. #define ttblsz 16384
  40. #define huge
  41. #endif MSDOS
  42.  
  43.  
  44. #define neutral 2
  45. #define white 0
  46. #define black 1 
  47. #define no_piece 0
  48. #define pawn 1
  49. #define knight 2
  50. #define bishop 3
  51. #define rook 4
  52. #define queen 5
  53. #define king 6
  54. #define valueP 100
  55. #define valueN 350
  56. #define valueB 355
  57. #define valueR 550
  58. #define valueQ 1100
  59. #define valueK 1200
  60. #define ctlP 0x4000
  61. #define ctlN 0x2800
  62. #define ctlB 0x1800
  63. #define ctlR 0x0400
  64. #define ctlQ 0x0200
  65. #define ctlK 0x0100
  66. #define ctlBQ 0x1200
  67. #define ctlRQ 0x0600
  68. #define ctlNN 0x2000
  69. #define pxx " PNBRQK"
  70. #define qxx " pnbrqk"
  71. #define rxx "12345678"
  72. #define cxx "abcdefgh"
  73. #define check 0x0001
  74. #define capture 0x0002
  75. #define draw 0x0004
  76. #define promote 0x0008
  77. #define cstlmask 0x0010
  78. #define epmask 0x0020
  79. #define exact 0x0040
  80. #define pwnthrt 0x0080
  81. #define truescore 0x0001
  82. #define lowerbound 0x0002
  83. #define upperbound 0x0004
  84. #define maxdepth 30
  85. #define true 1
  86. #define false 0
  87. #define absv(x) ((x) < 0 ? -(x) : (x))
  88. #define taxicab(a,b) (abs(column[a]-column[b]) + abs(row[a]-row[b]))
  89.  
  90. struct leaf
  91.   {
  92.     short f,t,score,reply;
  93.     unsigned short flags;
  94.   };
  95. struct GameRec
  96.   {
  97.     unsigned short gmove;
  98.     short score,depth,time,piece,color;
  99.     long nodes;
  100.   };
  101. struct TimeControlRec
  102.   {
  103.     short moves[2];
  104.     long clock[2];
  105.   };
  106. struct BookEntry
  107.   {
  108.     struct BookEntry *next;
  109.     unsigned short *mv;
  110.   };
  111. struct hashval
  112.   {
  113.     unsigned long bd;
  114.     unsigned short key;
  115.   };
  116. struct hashentry
  117.   {
  118.     unsigned long hashbd;
  119.     unsigned short mv,flags;
  120.     short score,depth;
  121.   };
  122.  
  123. char mvstr1[5],mvstr2[5];
  124. struct leaf Tree[2000],*root;
  125. short TrPnt[maxdepth],board[64],color[64];
  126. short row[64],column[64],locn[8][8],Pindex[64],svalue[64];
  127. short PieceList[2][16],PieceCnt[2],atak[2][64],PawnCnt[2][8];
  128. short castld[2],kingmoved[2],mtl[2],pmtl[2],emtl[2],hung[2];
  129. short c1,c2,*atk1,*atk2,*PC1,*PC2,EnemyKing;
  130. short mate,post,opponent,computer,Sdepth,Awindow,Bwindow,dither;
  131. long ResponseTime,ExtraTime,Level,et,et0,time0,cputimer,ft;
  132. long NodeCnt,evrate,ETnodes,EvalNodes,HashCnt;
  133. short quit,reverse,bothsides,hashflag,InChk,player,force,easy,beep;
  134. short wking,bking,FROMsquare,TOsquare,timeout,Zscore,zwndw,xwndw,slk;
  135. short INCscore;
  136. short HasPawn[2],HasKnight[2],HasBishop[2],HasRook[2],HasQueen[2];
  137. short ChkFlag[maxdepth],CptrFlag[maxdepth],PawnThreat[maxdepth];
  138. short Pscore[maxdepth],Tscore[maxdepth],Threat[maxdepth];
  139. struct GameRec GameList[240];
  140. short GameCnt,Game50,epsquare,lpost,rcptr,contempt;
  141. short MaxSearchDepth;
  142. struct BookEntry *Book;
  143. struct TimeControlRec TimeControl;
  144. short TCflag,TCmoves,TCminutes,OperatorTime;
  145. short otherside[3]={1,0,2};
  146. short rank7[3]={6,1,0};
  147. short map[64]=
  148.    {0,1,2,3,4,5,6,7,
  149.     0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,
  150.     0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,
  151.     0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,
  152.     0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,
  153.     0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,
  154.     0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,
  155.     0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77};
  156. short unmap[120]=
  157.    {0,1,2,3,4,5,6,7,-1,-1,-1,-1,-1,-1,-1,-1,
  158.     8,9,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,
  159.     16,17,18,19,20,21,22,23,-1,-1,-1,-1,-1,-1,-1,-1,
  160.     24,25,26,27,28,29,30,31,-1,-1,-1,-1,-1,-1,-1,-1,
  161.     32,33,34,35,36,37,38,39,-1,-1,-1,-1,-1,-1,-1,-1,
  162.     40,41,42,43,44,45,46,47,-1,-1,-1,-1,-1,-1,-1,-1,
  163.     48,49,50,51,52,53,54,55,-1,-1,-1,-1,-1,-1,-1,-1,
  164.     56,57,58,59,60,61,62,63};
  165. short Dcode[120]= 
  166.    {0,1,1,1,1,1,1,1,0,0,0,0,0,0,0x0E,0x0F,
  167.     0x10,0x11,0x12,0,0,0,0,0,0,0,0,0,0,0,0x0F,0x1F,
  168.     0x10,0x21,0x11,0,0,0,0,0,0,0,0,0,0,0x0F,0,0,
  169.     0x10,0,0,0x11,0,0,0,0,0,0,0,0,0x0F,0,0,0,
  170.     0x10,0,0,0,0x11,0,0,0,0,0,0,0x0F,0,0,0,0,
  171.     0x10,0,0,0,0,0x11,0,0,0,0,0x0F,0,0,0,0,0,
  172.     0x10,0,0,0,0,0,0x11,0,0,0x0F,0,0,0,0,0,0,
  173.     0x10,0,0,0,0,0,0,0x11};
  174. short Stboard[64]=
  175.    {rook,knight,bishop,queen,king,bishop,knight,rook,
  176.     pawn,pawn,pawn,pawn,pawn,pawn,pawn,pawn,
  177.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  178.     pawn,pawn,pawn,pawn,pawn,pawn,pawn,pawn,
  179.     rook,knight,bishop,queen,king,bishop,knight,rook};
  180. short Stcolor[64]=
  181.    {white,white,white,white,white,white,white,white,
  182.     white,white,white,white,white,white,white,white,
  183.     2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
  184.     black,black,black,black,black,black,black,black,
  185.     black,black,black,black,black,black,black,black};
  186. short sweep[7]= {false,false,false,true,true,true,false};
  187. short Dpwn[3]={4,6,0};
  188. short Dstart[7]={6,4,8,4,0,0,0};
  189. short Dstop[7]={7,5,15,7,3,7,7};
  190. short Dir[16]={1,0x10,-1,-0x10,0x0F,0x11,-0x0F,-0x11,
  191.                0x0E,-0x0E,0x12,-0x12,0x1F,-0x1F,0x21,-0x21};
  192. short Pdir[34]={0,0x38,0,0,0,0,0,0,0,0,0,0,0,0,0x02,0x35,
  193.                 0x38,0x35,0x02,0,0,0,0,0,0,0,0,0,0,0,0,0x02,
  194.                 0,0x02};
  195. short pbit[7]={0,0x01,0x02,0x04,0x08,0x10,0x20};
  196. unsigned short killr0[maxdepth],killr1[maxdepth],killr2[maxdepth];
  197. unsigned short killr3[maxdepth],PrVar[maxdepth];
  198. unsigned short PV,hint,Swag0,Swag1,Swag2,Swag3,Swag4;
  199. unsigned short hashkey;
  200. unsigned long hashbd;
  201. struct hashval hashcode[2][7][64];
  202. struct hashentry huge *ttable,*ptbl;
  203. unsigned char history[8192];
  204.  
  205. short Mwpawn[64],Mbpawn[64],Mknight[2][64],Mbishop[2][64];
  206. short Mking[2][64],Kfield[2][64];
  207. short value[7]={0,valueP,valueN,valueB,valueR,valueQ,valueK};
  208. short control[7]={0,ctlP,ctlN,ctlB,ctlR,ctlQ,ctlK};
  209. short PassedPawn0[8]={0,60,80,120,200,360,600,800};
  210. short PassedPawn1[8]={0,30,40,60,100,180,300,800};
  211. short PassedPawn2[8]={0,15,25,35,50,90,140,800};
  212. short PassedPawn3[8]={0,5,10,15,20,30,140,800};
  213. short ISOLANI[8] = {-12,-16,-20,-24,-24,-20,-16,-12};
  214. short BACKWARD[8] = {-6,-10,-15,-21,-28,-28,-28,-28};
  215. short BMBLTY[14] = {-2,0,2,4,6,8,10,12,13,14,15,16,16,16};
  216. short RMBLTY[14] = {0,2,4,6,8,10,11,12,13,14,14,14,14,14};
  217. short Kthreat[16] = {0,-8,-20,-36,-52,-68,-80,-80,-80,-80,-80,-80,
  218.                      -80,-80,-80,-80};
  219. short KNIGHTPOST,KNIGHTSTRONG,BISHOPSTRONG,KATAK,KBNKsq;
  220. short PEDRNK2B,PWEAKH,PADVNCM,PADVNCI,PAWNSHIELD,PDOUBLED,PBLOK;
  221. short RHOPN,RHOPNX,KHOPN,KHOPNX,KSFTY;
  222. short ATAKD,HUNGP,HUNGX,KCASTLD,KMOVD,XRAY,PINVAL;
  223. short stage,stage2,Zwmtl,Zbmtl,Developed[2],PawnStorm;
  224. short PawnBonus,BishopBonus,RookBonus;
  225. short KingOpening[64]=
  226.    {  0,  0, -4,-10,-10, -4,  0,  0,
  227.      -4, -4, -8,-12,-12, -8, -4, -4,
  228.     -12,-16,-20,-20,-20,-20,-16,-12,
  229.     -16,-20,-24,-24,-24,-24,-20,-16,
  230.     -16,-20,-24,-24,-24,-24,-20,-16,
  231.     -12,-16,-20,-20,-20,-20,-16,-12,
  232.      -4, -4, -8,-12,-12, -8, -4, -4,
  233.       0,  0, -4,-10,-10, -4,  0,  0};
  234. short KingEnding[64]=
  235.    { 0, 6,12,18,18,12, 6, 0,
  236.      6,12,18,24,24,18,12, 6,
  237.     12,18,24,30,30,24,18,12,
  238.     18,24,30,36,36,30,24,18,
  239.     18,24,30,36,36,30,24,18,
  240.     12,18,24,30,30,24,18,12,
  241.      6,12,18,24,24,18,12, 6,
  242.      0, 6,12,18,18,12, 6, 0};
  243. short DyingKing[64]=
  244.    { 0, 8,16,24,24,16, 8, 0,
  245.      8,32,40,48,48,40,32, 8,
  246.     16,40,56,64,64,56,40,16,
  247.     24,48,64,72,72,64,48,24,
  248.     24,48,64,72,72,64,48,24,
  249.     16,40,56,64,64,56,40,16,
  250.      8,32,40,48,48,40,32, 8,
  251.      0, 8,16,24,24,16, 8, 0};
  252. short KBNK[64]=
  253.    {99,90,80,70,60,50,40,40,
  254.     90,80,60,50,40,30,20,40,
  255.     80,60,40,30,20,10,30,50,
  256.     70,50,30,10, 0,20,40,60,
  257.     60,40,20, 0,10,30,50,70,
  258.     50,30,10,20,30,40,60,80,
  259.     40,20,30,40,50,60,80,90,
  260.     40,40,50,60,70,80,90,99};
  261. short pknight[64]=
  262.    { 0, 4, 8,10,10, 8, 4, 0,
  263.      4, 8,16,20,20,16, 8, 4,
  264.      8,16,24,28,28,24,16, 8,
  265.     10,20,28,32,32,28,20,10,
  266.     10,20,28,32,32,28,20,10,
  267.      8,16,24,28,28,24,16, 8,
  268.      4, 8,16,20,20,16, 8, 4,
  269.      0, 4, 8,10,10, 8, 4, 0};
  270. short pbishop[64]=
  271.    {14,14,14,14,14,14,14,14,
  272.     14,22,18,18,18,18,22,14,
  273.     14,18,22,22,22,22,18,14,
  274.     14,18,22,22,22,22,18,14,
  275.     14,18,22,22,22,22,18,14,
  276.     14,18,22,22,22,22,18,14,
  277.     14,22,18,18,18,18,22,14,
  278.     14,14,14,14,14,14,14,14};
  279. short PawnAdvance[64]=
  280.    { 0, 0, 0, 0, 0, 0, 0, 0,
  281.      4, 4, 4, 0, 0, 4, 4, 4,
  282.      6, 8, 2,10,10, 2, 8, 6,
  283.      6, 8,12,16,16,12, 8, 6,
  284.      8,12,16,24,24,16,12, 8,
  285.     12,16,24,32,32,24,16,12,
  286.     12,16,24,32,32,24,16,12,
  287.      0, 0, 0, 0, 0, 0, 0, 0};
  288.      
  289.      
  290.  
  291. main(argc,argv)
  292. int argc; char *argv[];
  293. {
  294. #ifdef MSDOS
  295.   ttable = (struct hashentry huge *)farmalloc(ttblsz *
  296.            (unsigned long)sizeof(struct hashentry));
  297. #else
  298.   ttable = (struct hashentry *)malloc(ttblsz *
  299.            (unsigned long)sizeof(struct hashentry));
  300. #endif
  301.   Level = 0; TCflag = false; OperatorTime = 0;
  302.   if (argc == 2) Level = atoi(argv[1]);
  303.   if (argc == 3)
  304.     {
  305.       TCmoves = atoi(argv[1]); TCminutes = atoi(argv[2]); TCflag = true;
  306.     }
  307.   Initialize();
  308.   NewGame();
  309.   while (!(quit))
  310.     {
  311.       if (bothsides && !mate) SelectMove(opponent,1); else InputCommand();
  312.       if (!(quit || mate || force)) SelectMove(computer,1);
  313.     }
  314.   ExitChess();
  315. }
  316.  
  317.  
  318.  
  319. /* ............    INTERFACE ROUTINES    ........................... */
  320.  
  321. int VerifyMove(s,iop,mv)
  322. char s[];
  323. short iop;
  324. unsigned short *mv;
  325.  
  326. /*
  327.    Compare the string 's' to the list of legal moves available for the 
  328.    opponent. If a match is found, make the move on the board. 
  329. */
  330.  
  331. {
  332. static short pnt,tempb,tempc,tempsf,tempst,cnt;
  333. static struct leaf xnode;
  334. struct leaf *node;
  335.  
  336.   *mv = 0;
  337.   if (iop == 2)
  338.     {
  339.       UnmakeMove(opponent,&xnode,&tempb,&tempc,&tempsf,&tempst);
  340.       return(false);
  341.     }
  342.   cnt = 0;
  343.   MoveList(opponent,2);
  344.   pnt = TrPnt[2];
  345.   while (pnt < TrPnt[3])
  346.     {
  347.       node = &Tree[pnt++];
  348.       algbr(node->f,node->t,node->flags & cstlmask);
  349.       if (strcmp(s,mvstr1) == 0 || strcmp(s,mvstr2) == 0)
  350.         {
  351.           cnt++; xnode = *node;
  352.         }
  353.     }
  354.   if (cnt == 1)
  355.     {
  356.       MakeMove(opponent,&xnode,&tempb,&tempc,&tempsf,&tempst);
  357.       if (SqAtakd(PieceList[opponent][0],computer))
  358.         {
  359.           UnmakeMove(opponent,&xnode,&tempb,&tempc,&tempsf,&tempst);
  360.           ShowMessage("Illegal Move!!");
  361.           return(false);
  362.         }
  363.       else
  364.         {
  365.           if (iop == 1) return(true);
  366.           if (xnode.flags & epmask) UpdateDisplay(0,0,1,0);
  367.           else UpdateDisplay(xnode.f,xnode.t,0,xnode.flags & cstlmask);
  368.           if (xnode.flags & cstlmask) Game50 = GameCnt;
  369.           else if (board[xnode.t] == pawn || (xnode.flags & capture)) 
  370.             Game50 = GameCnt;
  371.           GameList[GameCnt].depth = GameList[GameCnt].score = 0;
  372.           GameList[GameCnt].nodes = 0;
  373.           ElapsedTime(1);
  374.           GameList[GameCnt].time = (short)et;
  375.           TimeControl.clock[opponent] -= et;
  376.           --TimeControl.moves[opponent];
  377.           *mv = (xnode.f << 8) + xnode.t;
  378.           algbr(xnode.f,xnode.t,false);
  379.           return(true);
  380.         } 
  381.     }
  382.   if (cnt > 1) ShowMessage("Ambiguous Move!");
  383.   return(false);
  384. }
  385.  
  386.  
  387. NewGame()
  388.  
  389. /*
  390.    Reset the board and other variables to start a new game.
  391. */
  392.  
  393. {
  394. short l,r,c,p;
  395.  
  396.   mate = quit = reverse = bothsides = post = false;
  397.   hashflag = force = PawnStorm = false;
  398.   beep = rcptr = easy = true;
  399.   lpost =  NodeCnt = epsquare = et0 = 0;
  400.   dither = 0;
  401.   Awindow = 90;
  402.   Bwindow = 90;
  403.   xwndw = 90;
  404.   MaxSearchDepth = 29;
  405.   contempt = 0;
  406.   GameCnt = -1; Game50 = 0;
  407.   Zwmtl = Zbmtl = 0;
  408.   Developed[white] = Developed[black] = false;
  409.   castld[white] = castld[black] = false;
  410.   kingmoved[white] = kingmoved[black] = 0;
  411.   PawnThreat[0] = CptrFlag[0] = Threat[0] = false;
  412.   Pscore[0] = 12000; Tscore[0] = 12000;
  413.   opponent = white; computer = black;
  414.   for (r = 0; r < 8; r++)
  415.     for (c = 0; c < 8; c++)
  416.       {
  417.         l = 8*r+c; locn[r][c] = l;
  418.         row[l] = r; column[l] = c;
  419.         board[l] = Stboard[l]; color[l] = Stcolor[l];
  420.       }
  421.   for (c = white; c <= black; c++)
  422.     for (p = pawn; p <= king; p++)
  423.       for (l = 0; l < 64; l++)
  424.         {
  425.           hashcode[c][p][l].key = (unsigned short)rand();
  426.           hashcode[c][p][l].bd = ((unsigned long)rand() << 16) +
  427.                                  (unsigned long)rand();
  428.         }
  429.   ClrScreen();
  430.   if (TCflag) SetTimeControl();
  431.   else if (Level == 0) SelectLevel();
  432.   UpdateDisplay(0,0,1,0);
  433.   InitializeStats();
  434.   time0 = time((long *)0);
  435.   ElapsedTime(1);
  436.   GetOpenings();
  437. }
  438.  
  439.  
  440. algbr(f,t,iscastle)
  441. short f,t,iscastle;
  442. {
  443.   mvstr1[0] = cxx[column[f]]; mvstr1[1] = rxx[row[f]];
  444.   mvstr1[2] = cxx[column[t]]; mvstr1[3] = rxx[row[t]];
  445.   mvstr2[0] = qxx[board[f]];
  446.   mvstr2[1] = mvstr1[2]; mvstr2[2] = mvstr1[3];
  447.   mvstr1[4] = '\0'; mvstr2[3] = '\0';
  448.   if (iscastle)
  449.     if (t > f) strcpy(mvstr2,"o-o");
  450.     else strcpy(mvstr2,"o-o-o");
  451. }
  452.  
  453.  
  454. /* ............    MOVE GENERATION & SEARCH ROUTINES    .............. */
  455.  
  456. SelectMove(side,iop)
  457. short side,iop;
  458.  
  459. /*
  460.    Select a move by calling function search() at progressively deeper 
  461.    ply until time is up or a mate or draw is reached. An alpha-beta 
  462.    window of -90 to +90 points is set around the score returned from the 
  463.    previous iteration. If Sdepth != 0 then the program has correctly 
  464.    predicted the opponents move and the search will start at a depth of 
  465.    Sdepth+1 rather than a depth of 1. 
  466. */
  467.  
  468. {
  469. static short i,alpha,beta,score,tempb,tempc,tempsf,tempst,xside,rpt;
  470.  
  471.   timeout = false;
  472.   xside = otherside[side];
  473.   if (iop != 2) player = side;
  474.   if (TCflag)
  475.     {
  476.       if (((TimeControl.moves[side] + 3) - OperatorTime) != 0)
  477.         ResponseTime = (TimeControl.clock[side]) /
  478.                        (TimeControl.moves[side] + 3) -
  479.                        OperatorTime;
  480.       else ResponseTime = 0;
  481.       ResponseTime += (ResponseTime*TimeControl.moves[side])/(2*TCmoves+1);
  482.     }
  483.   else ResponseTime = Level;
  484.   if (iop == 2) ResponseTime = 999;
  485.   if (Sdepth > 0 && root->score > Zscore-zwndw) ResponseTime -= ft;
  486.   else if (ResponseTime < 1) ResponseTime = 1;
  487.   ExtraTime = 0;
  488.   ExaminePosition();
  489.   ScorePosition(side,&score);
  490.   ShowSidetomove();
  491.   
  492.   if (Sdepth == 0)
  493.   {
  494.     ZeroTTable();
  495.     SearchStartStuff(side);
  496.     for (i = 0; i < 8192; i++) history[i] = 0;
  497.     FROMsquare = TOsquare = -1;
  498.     PV = 0;
  499.     if (iop != 2) hint = 0;
  500.     for (i = 0; i < maxdepth; i++)
  501.      PrVar[i] = killr0[i] = killr1[i] = killr2[i] = killr3[i] = 0;
  502.     alpha = score-90; beta = score+90;
  503.     rpt = 0;
  504.     TrPnt[1] = 0; root = &Tree[0];
  505.     MoveList(side,1);
  506.     for (i = TrPnt[1]; i < TrPnt[2]; i++) pick(i,TrPnt[2]-1);
  507.     if (Book != NULL) OpeningBook();
  508.     if (Book != NULL) timeout = true;
  509.     NodeCnt = ETnodes = EvalNodes = HashCnt = 0;
  510.     Zscore = 0; zwndw = 20;
  511.   }
  512.   
  513.   while (!timeout && Sdepth < MaxSearchDepth)
  514.     {
  515.       Sdepth++;
  516.       ShowDepth(' ');
  517.       score = search(side,1,Sdepth,alpha,beta,PrVar,&rpt);
  518.       for (i = 1; i <= Sdepth; i++) killr0[i] = PrVar[i];
  519.       if (score < alpha)
  520.         {
  521.           ShowDepth('-');
  522.           ExtraTime = 10*ResponseTime;
  523.           ZeroTTable();
  524.           score = search(side,1,Sdepth,-9000,beta,PrVar,&rpt);
  525.         }
  526.       if (score > beta && !(root->flags & exact))
  527.         {
  528.           ShowDepth('+');
  529.           ExtraTime = 0;
  530.           ZeroTTable();
  531.           score = search(side,1,Sdepth,alpha,9000,PrVar,&rpt);
  532.         }
  533.       score = root->score;
  534.       if (!timeout)
  535.         for (i = TrPnt[1]+1; i < TrPnt[2]; i++) pick(i,TrPnt[2]-1);
  536.       ShowResults(score,PrVar,'.');
  537.       for (i = 1; i <= Sdepth; i++) killr0[i] = PrVar[i];
  538.       if (score > Zscore-zwndw && score > Tree[1].score+250) ExtraTime = 0;
  539.       else if (score > Zscore-3*zwndw) ExtraTime = ResponseTime;
  540.       else ExtraTime = 3*ResponseTime;
  541.       if (root->flags & exact) timeout = true;
  542.       if (Tree[1].score < -9000) timeout = true;
  543.       if (4*et > 2*ResponseTime + ExtraTime) timeout = true;
  544.       if (!timeout)
  545.         {
  546.           Tscore[0] = score;
  547.           if (Zscore == 0) Zscore = score;
  548.           else Zscore = (Zscore+score)/2;
  549.         }
  550.       zwndw = 20+abs(Zscore/12);
  551.       beta = score + Bwindow;
  552.       if (Zscore < score) alpha = Zscore - Awindow - zwndw;
  553.       else alpha = score - Awindow - zwndw;
  554.     }
  555.  
  556.   score = root->score;
  557.   if (rpt >= 2 || score < -12000) root->flags |= draw;
  558.   if (iop == 2) return(0);
  559.   if (Book == NULL) hint = PrVar[2];
  560.   ElapsedTime(1);
  561.  
  562.   if (score > -9999 && rpt <= 2)
  563.     {
  564.       MakeMove(side,root,&tempb,&tempc,&tempsf,&tempst);
  565.       algbr(root->f,root->t,root->flags & cstlmask);
  566.     }
  567.   else mvstr1[0] = '\0';
  568.   OutputMove();
  569.   if (score == -9999 || score == 9998) mate = true;
  570.   if (mate) hint = 0;
  571.   if (root->flags & cstlmask) Game50 = GameCnt;
  572.   else if (board[root->t] == pawn || (root->flags & capture)) 
  573.     Game50 = GameCnt;
  574.   GameList[GameCnt].score = score;
  575.   GameList[GameCnt].nodes = NodeCnt;
  576.   GameList[GameCnt].time = (short)et;
  577.   GameList[GameCnt].depth = Sdepth;
  578.   if (TCflag)
  579.     {
  580.       TimeControl.clock[side] -= (et + OperatorTime);
  581.       if (--TimeControl.moves[side] == 0) SetTimeControl();
  582.     }
  583.   if ((root->flags & draw) && bothsides) quit = true;
  584.   if (GameCnt > 238) quit = true;
  585.   player = xside;
  586.   Sdepth = 0;
  587.   fflush(stdin);
  588.   return(0);
  589. }
  590.  
  591.  
  592. OpeningBook()
  593.  
  594. /*
  595.    Go thru each of the opening lines of play and check for a match with 
  596.    the current game listing. If a match occurs, generate a random number. 
  597.    If this number is the largest generated so far then the next move in 
  598.    this line becomes the current "candidate". After all lines are 
  599.    checked, the candidate move is put at the top of the Tree[] array and 
  600.    will be played by the program. Note that the program does not handle 
  601.    book transpositions. 
  602. */
  603.  
  604. {
  605. short j,pnt;
  606. unsigned short m,*mp;
  607. unsigned r,r0;
  608. struct BookEntry *p;
  609.  
  610.   srand((unsigned)time0);
  611.   r0 = m = 0;
  612.   p = Book;
  613.   while (p != NULL)
  614.     {
  615.       mp = p->mv;
  616.       for (j = 0; j <= GameCnt; j++)
  617.         if (GameList[j].gmove != *(mp++)) break;
  618.       if (j > GameCnt)
  619.         if ((r=rand()) > r0)
  620.           {
  621.             r0 = r; m = *mp;
  622.             hint = *(++mp);
  623.           }
  624.       p = p->next;
  625.     }
  626.     
  627.   for (pnt = TrPnt[1]; pnt < TrPnt[2]; pnt++)
  628.     if ((Tree[pnt].f<<8) + Tree[pnt].t == m) Tree[pnt].score = 0;
  629.   pick(TrPnt[1],TrPnt[2]-1);
  630.   if (Tree[TrPnt[1]].score < 0) Book = NULL;
  631. }
  632.  
  633.  
  634. #define UpdateSearchStatus\
  635. {\
  636.   if (post) ShowCurrentMove(pnt,node->f,node->t);\
  637.   if (pnt > TrPnt[1])\
  638.     {\
  639.       d = best-Zscore; e = best-node->score;\
  640.       if (best < alpha) ExtraTime = 10*ResponseTime;\
  641.       else if (d > -zwndw && e > 4*zwndw) ExtraTime = -ResponseTime/3;\
  642.       else if (d > -zwndw) ExtraTime = 0;\
  643.       else if (d > -3*zwndw) ExtraTime = ResponseTime;\
  644.       else if (d > -9*zwndw) ExtraTime = 3*ResponseTime;\
  645.       else ExtraTime = 5*ResponseTime;\
  646.     }\
  647. }
  648.  
  649. int search(side,ply,depth,alpha,beta,bstline,rpt)
  650. short side,ply,depth,alpha,beta,*rpt;
  651. unsigned short bstline[];
  652.  
  653. /*
  654.    Perform an alpha-beta search to determine the score for the current 
  655.    board position. If depth <= 0 only capturing moves, pawn promotions 
  656.    and responses to check are generated and searched, otherwise all 
  657.    moves are processed. The search depth is modified for check evasions, 
  658.    certain re-captures and threats. Extensions may continue for up to 11 
  659.    ply beyond the nominal search depth. 
  660. */
  661.  
  662. #define prune (cf && score+node->score < alpha)
  663. #define ReCapture (rcptr && score > alpha && score < beta &&\
  664.                    ply > 2 && CptrFlag[ply-1] && CptrFlag[ply-2])
  665. #define MateThreat (ply < Sdepth+4 && ply > 4 &&\
  666.                     ChkFlag[ply-2] && ChkFlag[ply-4] &&\
  667.                     ChkFlag[ply-2] != ChkFlag[ply-4])
  668.  
  669. {
  670. register short j,pnt;
  671. short best,tempb,tempc,tempsf,tempst;
  672. short xside,pbst,d,e,cf,score,rcnt;
  673. unsigned short mv,nxtline[maxdepth];
  674. struct leaf *node,tmp;
  675.  
  676.   NodeCnt++;
  677.   xside = otherside[side];
  678.   if (depth < 0) depth = 0;
  679.   
  680.   if (ply <= Sdepth+3) repetition(rpt); else *rpt = 0;
  681.   if (*rpt >= 2) return(0);
  682.  
  683.   score = evaluate(side,xside,ply,alpha,beta);
  684.   if (score > 9000)
  685.     {
  686.       bstline[ply] = 0;
  687.       return(score);
  688.     }
  689.                 
  690.   if (depth > 0)
  691.     {
  692.       if (InChk || PawnThreat[ply-1] || ReCapture) ++depth;
  693.     }
  694.   else
  695.     {
  696.       if (score >= alpha &&
  697.          (InChk || PawnThreat[ply-1] || Threat[ply-1])) ++depth;
  698.       else if (score <= beta && MateThreat) ++depth;
  699.     }
  700.     
  701.   if (depth > 0 && hashflag && ply > 1)
  702.     {
  703.       ProbeTTable(side,depth,&alpha,&beta,&score);
  704.       bstline[ply] = PV;
  705.       bstline[ply+1] = 0;
  706.       if (beta == -20000) return(score);
  707.       if (alpha > beta) return(alpha);
  708.     }
  709.     
  710.   if (Sdepth == 1) d = 7; else d = 11;
  711.   if (ply > Sdepth+d || (depth < 1 && score > beta)) return(score);
  712.  
  713.   if (ply > 1)
  714.     if (depth > 0) MoveList(side,ply);
  715.     else CaptureList(side,xside,ply);
  716.     
  717.   if (TrPnt[ply] == TrPnt[ply+1]) return(score);
  718.     
  719.   cf = (depth < 1 && ply > Sdepth+1 && !ChkFlag[ply-2] && !slk);
  720.  
  721.   if (depth > 0) best = -12000; else best = score;
  722.   if (best > alpha) alpha = best;
  723.   
  724.   for (pnt = pbst = TrPnt[ply];
  725.        pnt < TrPnt[ply+1] && best <= beta;
  726.        pnt++)
  727.     {
  728.       if (ply > 1) pick(pnt,TrPnt[ply+1]-1);
  729.       node = &Tree[pnt];
  730.       mv = (node->f << 8) + node->t;
  731.       nxtline[ply+1] = 0;
  732.       
  733.       if (prune) break;
  734.       if (ply == 1) UpdateSearchStatus;
  735.  
  736.       if (!(node->flags & exact))
  737.         {
  738.           MakeMove(side,node,&tempb,&tempc,&tempsf,&tempst);
  739.           CptrFlag[ply] = (node->flags & capture);
  740.           PawnThreat[ply] = (node->flags & pwnthrt);
  741.           Tscore[ply] = node->score;
  742.           PV = node->reply;
  743.           node->score = -search(xside,ply+1,depth-1,-beta,-alpha,
  744.                                 nxtline,&rcnt);
  745.           if (abs(node->score) > 9000) node->flags |= exact;
  746.           else if (rcnt == 1) node->score /= 2;
  747.           if (rcnt >= 2 || GameCnt-Game50 > 99 ||
  748.              (node->score == 9999-ply && !ChkFlag[ply]))
  749.             {
  750.               node->flags |= draw; node->flags |= exact;
  751.               if (side == computer) node->score = contempt;
  752.               else node->score = -contempt;
  753.             }
  754.           node->reply = nxtline[ply+1];
  755.           UnmakeMove(side,node,&tempb,&tempc,&tempsf,&tempst);
  756.         }
  757.       if (node->score > best && !timeout)
  758.         {
  759.           if (depth > 0)
  760.             if (node->score > alpha && !(node->flags & exact))
  761.               node->score += depth;
  762.           best = node->score; pbst = pnt;
  763.           if (best > alpha) alpha = best;
  764.           for (j = ply+1; nxtline[j] > 0; j++) bstline[j] = nxtline[j];
  765.           bstline[j] = 0;
  766.           bstline[ply] = mv;
  767.           if (ply == 1)
  768.             {
  769.               if (best == alpha)
  770.                 {
  771.                   tmp = Tree[pnt];
  772.                   for (j = pnt-1; j >= 0; j--) Tree[j+1] = Tree[j];
  773.                   Tree[0] = tmp;
  774.                   pbst = 0;
  775.                 }
  776.               if (Sdepth > 2)
  777.                 if (best > beta) ShowResults(best,bstline,'+');
  778.                 else if (best < alpha) ShowResults(best,bstline,'-');
  779.                 else ShowResults(best,bstline,'&');
  780.             }
  781.         }
  782.       if (NodeCnt > ETnodes) ElapsedTime(0);
  783.       if (timeout) return(-Tscore[ply-1]);
  784.     }
  785.     
  786.   node = &Tree[pbst];
  787.   mv = (node->f<<8) + node->t;
  788.   if (hashflag && ply <= Sdepth && *rpt == 0 && best == alpha)
  789.     PutInTTable(side,best,depth,alpha,beta,mv);
  790.   if (depth > 0)
  791.     {
  792.       j = (node->f<<6) + node->t; if (side == black) j |= 0x1000;
  793.       if (history[j] < 150) history[j] += 2*depth;
  794.       if (node->t != (GameList[GameCnt].gmove & 0xFF))
  795.         if (best <= beta) killr3[ply] = mv;
  796.         else if (mv != killr1[ply])
  797.           {
  798.             killr2[ply] = killr1[ply];
  799.             killr1[ply] = mv;
  800.           }
  801.       if (best > 9000) killr0[ply] = mv; else killr0[ply] = 0;
  802.     }
  803.   return(best);
  804. }
  805.  
  806.  
  807. evaluate(side,xside,ply,alpha,beta)
  808. short side,xside,ply,alpha,beta;
  809.  
  810. /*
  811.    Compute an estimate of the score by adding the positional score from 
  812.    the previous ply to the material difference. If this score falls 
  813.    inside a window which is 180 points wider than the alpha-beta window 
  814.    (or within a 50 point window during quiescence search) call 
  815.    ScorePosition() to determine a score, otherwise return the estimated 
  816.    score. If one side has only a king and the other either has no pawns 
  817.    or no pieces then the function ScoreLoneKing() is called. 
  818. */
  819.  
  820. {
  821. short s,evflag;
  822.  
  823.   hung[white] = hung[black] = 0;
  824.   slk = ((mtl[white] == valueK && (pmtl[black] == 0 || emtl[black] == 0)) ||
  825.          (mtl[black] == valueK && (pmtl[white] == 0 || emtl[white] == 0)));
  826.   s = -Pscore[ply-1] + mtl[side] - mtl[xside];
  827.   s -= INCscore;
  828.   
  829.   if (slk) evflag = false;
  830.   else evflag = 
  831.      (ply == 1 || ply < Sdepth ||
  832.      ((ply == Sdepth+1 || ply == Sdepth+2) &&
  833.       (s > alpha-xwndw && s < beta+xwndw)) ||
  834.      (ply > Sdepth+2 && s >= alpha-25 && s <= beta+25));
  835.     
  836.   if (evflag)
  837.     {
  838.       EvalNodes++;
  839.       ataks(side,atak[side]);
  840.       if (atak[side][PieceList[xside][0]] > 0) return(10001-ply);
  841.       ataks(xside,atak[xside]);
  842.       InChk = (atak[xside][PieceList[side][0]] > 0);
  843.       ScorePosition(side,&s);
  844.     }
  845.   else
  846.     {
  847.       if (SqAtakd(PieceList[xside][0],side)) return(10001-ply);
  848.       InChk = SqAtakd(PieceList[side][0],xside);
  849.       if (slk) ScoreLoneKing(side,&s);
  850.     }
  851.     
  852.   Pscore[ply] = s - mtl[side] + mtl[xside];
  853.   if (InChk) ChkFlag[ply-1] = Pindex[TOsquare];
  854.   else ChkFlag[ply-1] = 0;
  855.   Threat[ply-1] = (hung[side] > 1 && ply == Sdepth+1);
  856.   return(s);
  857. }
  858.  
  859.  
  860. ProbeTTable(side,depth,alpha,beta,score)
  861. short side,depth,*alpha,*beta,*score;
  862.  
  863. /* 
  864.    Look for the current board position in the transposition table.
  865. */
  866.  
  867. {
  868. short hindx;
  869.   if (side == white) hashkey |= 1; else hashkey &= 0xFFFE;
  870.   hindx = (hashkey & (ttblsz-1));
  871.   ptbl = (ttable + hindx);
  872.   if (ptbl->depth >= depth && ptbl->hashbd == hashbd)
  873.     {
  874.       HashCnt++;
  875.       PV = ptbl->mv;
  876.       if (ptbl->flags & truescore)
  877.         {
  878.           *score = ptbl->score;
  879.           *beta = -20000;
  880.           return(true);
  881.         }
  882. /*
  883.       else if (ptbl->flags & upperbound)
  884.         {
  885.           if (ptbl->score < *beta) *beta = ptbl->score+1;
  886.         }
  887. */
  888.       else if (ptbl->flags & lowerbound)
  889.         {
  890.           if (ptbl->score > *alpha) *alpha = ptbl->score-1;
  891.         }
  892.     }
  893.   return(false);
  894. }
  895.  
  896.  
  897. PutInTTable(side,score,depth,alpha,beta,mv)
  898. short side,score,depth,alpha,beta;
  899. unsigned short mv;
  900.  
  901. /*
  902.    Store the current board position in the transposition table.
  903. */
  904.  
  905. {
  906. short hindx;
  907.   if (side == white) hashkey |= 1; else hashkey &= 0xFFFE;
  908.   hindx = (hashkey & (ttblsz-1));
  909.   ptbl = (ttable + hindx);
  910.   ptbl->hashbd = hashbd;
  911.   ptbl->depth = depth;
  912.   ptbl->score = score; 
  913.   ptbl->mv = mv;
  914.   ptbl->flags = 0;
  915.   if (score < alpha) ptbl->flags |= upperbound;
  916.   else if (score > beta) ptbl->flags |= lowerbound;
  917.   else ptbl->flags |= truescore;
  918. }
  919.  
  920.  
  921. ZeroTTable()
  922. {
  923. int i;
  924.   if (hashflag)
  925.     for (i = 0; i < ttblsz; i++)
  926.       {
  927.         ptbl = (ttable + i);
  928.         ptbl->depth = 0;
  929.       }
  930. }
  931.  
  932.  
  933. MoveList(side,ply)
  934. short side,ply;
  935.  
  936. /*
  937.    Fill the array Tree[] with all available moves for side to play. Array 
  938.    TrPnt[ply] contains the index into Tree[] of the first move at a ply. 
  939. */
  940.     
  941. {
  942. register short i;
  943. short xside,f;
  944.  
  945.   xside = otherside[side];
  946.   if (PV == 0) Swag0 = killr0[ply]; else Swag0 = PV;
  947.   Swag1 = killr1[ply]; Swag2 = killr2[ply];
  948.   Swag3 = killr3[ply]; Swag4 = 0;
  949.   if (ply > 2) Swag4 = killr1[ply-2];
  950.   TrPnt[ply+1] = TrPnt[ply];
  951.   Dstart[pawn] = Dpwn[side]; Dstop[pawn] = Dstart[pawn] + 1;
  952.   for (i = PieceCnt[side]; i >= 0; i--)
  953.     GenMoves(ply,PieceList[side][i],side,xside);
  954.   if (kingmoved[side] == 0 && !castld[side])
  955.     {
  956.       f = PieceList[side][0];
  957.       if (castle(side,f,f+2,0))
  958.         {
  959.           LinkMove(ply,f,f+2,xside);
  960.           Tree[TrPnt[ply+1]-1].flags |= cstlmask;
  961.         }
  962.       if (castle(side,f,f-2,0))
  963.         {
  964.           LinkMove(ply,f,f-2,xside);
  965.           Tree[TrPnt[ply+1]-1].flags |= cstlmask;
  966.         }
  967.     }
  968. }
  969.  
  970.  
  971. GenMoves(ply,sq,side,xside)
  972. short ply,sq,side,xside;
  973.  
  974. /*
  975.    Generate moves for a piece. The from square is mapped onto a special  
  976.    board and offsets (taken from array Dir[]) are added to the mapped 
  977.    location. The newly generated square is tested to see if it falls off 
  978.    the board by ANDing the square with 88 HEX. Legal moves are linked 
  979.    into the tree. 
  980. */
  981.     
  982. {
  983. register short m,u,d;
  984. short i,m0,piece; 
  985.  
  986.   piece = board[sq]; m0 = map[sq];
  987.   if (sweep[piece])
  988.     for (i = Dstart[piece]; i <= Dstop[piece]; i++)
  989.       {
  990.         d = Dir[i]; m = m0+d;
  991.         while (!(m & 0x88))
  992.           {
  993.             u = unmap[m];
  994.             if (color[u] == neutral)
  995.               {
  996.                 LinkMove(ply,sq,u,xside);
  997.                 m += d;
  998.               }
  999.             else if (color[u] == xside)
  1000.               {
  1001.                 LinkMove(ply,sq,u,xside);
  1002.                 break;
  1003.               }
  1004.             else break;
  1005.           }
  1006.       }
  1007.   else if (piece == pawn)
  1008.     {
  1009.       if (side == white && color[sq+8] == neutral)
  1010.         {
  1011.           LinkMove(ply,sq,sq+8,xside);
  1012.           if (row[sq] == 1)
  1013.             if (color[sq+16] == neutral)
  1014.               LinkMove(ply,sq,sq+16,xside);
  1015.         }
  1016.       else if (side == black && color[sq-8] == neutral)
  1017.         {
  1018.           LinkMove(ply,sq,sq-8,xside);
  1019.           if (row[sq] == 6)
  1020.             if (color[sq-16] == neutral)
  1021.               LinkMove(ply,sq,sq-16,xside);
  1022.         }
  1023.       for (i = Dstart[piece]; i <= Dstop[piece]; i++)
  1024.         if (!((m = m0+Dir[i]) & 0x88))
  1025.           {
  1026.             u = unmap[m];
  1027.             if (color[u] == xside || u == epsquare)
  1028.               LinkMove(ply,sq,u,xside);
  1029.           }
  1030.     }
  1031.   else
  1032.     {
  1033.       for (i = Dstart[piece]; i <= Dstop[piece]; i++)
  1034.         if (!((m = m0+Dir[i]) & 0x88))
  1035.           {
  1036.             u = unmap[m];
  1037.             if (color[u] != side) LinkMove(ply,sq,u,xside);
  1038.           }
  1039.     }
  1040. }
  1041.  
  1042.  
  1043. LinkMove(ply,f,t,xside)
  1044. short ply,f,t,xside;
  1045.  
  1046. /*
  1047.    Add a move to the tree.  Assign a bonus to order the moves
  1048.    as follows:
  1049.      1. Principle variation
  1050.      2. Capture of last moved piece
  1051.      3. Other captures (major pieces first)
  1052.      4. Killer moves
  1053.      5. "history" killers    
  1054. */
  1055.  
  1056. {
  1057. register short s,z;
  1058. unsigned short mv;
  1059. struct leaf *node;
  1060.  
  1061.   node = &Tree[TrPnt[ply+1]];
  1062.   ++TrPnt[ply+1];
  1063.   node->flags = node->reply = 0;
  1064.   node->f = f; node->t = t;
  1065.   mv = (f<<8) + t;
  1066.   s = 0;
  1067.   if (mv == Swag0) s = 2000;
  1068.   else if (mv == Swag1) s = 60;
  1069.   else if (mv == Swag2) s = 50;
  1070.   else if (mv == Swag3) s = 40;
  1071.   else if (mv == Swag4) s = 30;
  1072.   if (color[t] != neutral)
  1073.     {
  1074.       node->flags |= capture;
  1075.       if (t == TOsquare) s += 500;
  1076.       s += value[board[t]] - board[f];
  1077.     }
  1078.   if (board[f] == pawn)
  1079.     if (row[t] == 0 || row[t] == 7)
  1080.       {
  1081.         node->flags |= promote;
  1082.         s += 800;
  1083.       }
  1084.     else if (row[t] == 1 || row[t] == 6)
  1085.       {
  1086.         node->flags |= pwnthrt;
  1087.         s += 600;
  1088.       }
  1089.     else if (t == epsquare) node->flags |= epmask;
  1090.   z = (f<<6) + t; if (xside == white) z |= 0x1000;
  1091.   s += history[z];
  1092.   node->score = s - 20000;
  1093. }
  1094.  
  1095.  
  1096. CaptureList(side,xside,ply)
  1097. short side,xside,ply;
  1098.  
  1099. /*
  1100.     Generate captures and Pawn promotions only.
  1101. */
  1102.  
  1103. #define LinkCapture\
  1104. {\
  1105.   node->f = sq; node->t = u;\
  1106.   node->reply = 0;\
  1107.   node->flags = capture;\
  1108.   node->score = value[board[u]] + svalue[board[u]] - piece;\
  1109.   if (piece == pawn && (u < 8 || u > 55))\
  1110.     {\
  1111.       node->flags |= promote;\
  1112.       node->score = valueQ;\
  1113.     }\
  1114.   ++node;\
  1115.   ++TrPnt[ply+1];\
  1116. }
  1117.  
  1118. {
  1119. register short m,u;
  1120. short d,sq,i,j,j1,j2,m0,r7,d0,piece,*PL;
  1121. struct leaf *node;
  1122.  
  1123.   TrPnt[ply+1] = TrPnt[ply];
  1124.   node = &Tree[TrPnt[ply]];
  1125.   Dstart[pawn] = Dpwn[side]; Dstop[pawn] = Dstart[pawn] + 1;
  1126.   if (side == white)
  1127.     {
  1128.       r7 = 6; d0 = 8;
  1129.     }
  1130.   else
  1131.     {
  1132.       r7 = 1; d0 = -8;
  1133.     }
  1134.   PL = PieceList[side];
  1135.   for (i = 0; i <= PieceCnt[side]; i++)
  1136.     { 
  1137.       sq = PL[i];
  1138.       m0 = map[sq]; piece = board[sq];
  1139.       j1 = Dstart[piece]; j2 = Dstop[piece];
  1140.       if (sweep[piece])
  1141.         for (j = j1; j <= j2; j++)
  1142.           {
  1143.             d = Dir[j]; m = m0+d;
  1144.             while (!(m & 0x88))
  1145.               {
  1146.                 u = unmap[m];
  1147.                 if (color[u] == neutral) m += d;
  1148.                 else
  1149.                   {
  1150.                     if (color[u] == xside) LinkCapture;
  1151.                     break;
  1152.                   }
  1153.               }
  1154.           }
  1155.       else
  1156.         {
  1157.           for (j = j1; j <= j2; j++)
  1158.             if (!((m = m0+Dir[j]) & 0x88))
  1159.               {
  1160.                 u = unmap[m];
  1161.                 if (color[u] == xside) LinkCapture;
  1162.               }
  1163.           if (piece == pawn && row[sq] == r7)
  1164.             {
  1165.               u = sq+d0;
  1166.               if (color[u] == neutral) LinkCapture;
  1167.             }
  1168.         }
  1169.     }
  1170. }
  1171.  
  1172.   
  1173. int castle(side,kf,kt,iop)
  1174. short side,kf,kt,iop;
  1175.  
  1176. /*
  1177.    Make or Unmake a castling move.
  1178. */
  1179.  
  1180. {
  1181. short rf,rt,d,t0,xside;
  1182.  
  1183.   xside = otherside[side];
  1184.   if (kt > kf)
  1185.     {
  1186.       rf = kf+3; rt = kt-1; d = 1;
  1187.     }
  1188.   else
  1189.     {
  1190.       rf = kf-4; rt = kt+1; d = -1;
  1191.     }
  1192.   if (iop == 0)
  1193.     {
  1194.       if (board[kf] != king || board[rf] != rook || color[rf] != side)
  1195.         return(false);
  1196.       if (color[kt] != neutral || color[rt] != neutral) return(false);
  1197.       if (d == -1 && color[kt+d] != neutral) return(false);
  1198.       if (SqAtakd(kf,xside)) return(false);
  1199.       if (SqAtakd(kt,xside)) return(false);
  1200.       if (SqAtakd(kf+d,xside)) return(false);
  1201.     }
  1202.   else
  1203.     {
  1204.       if (iop == 1) castld[side] = true; else castld[side] = false;
  1205.       if (iop == 2)
  1206.         {
  1207.           t0 = kt; kt = kf; kf = t0;
  1208.           t0 = rt; rt = rf; rf = t0;
  1209.         }
  1210.       board[kt] = king; color[kt] = side; Pindex[kt] = 0;
  1211.       board[kf] = no_piece; color[kf] = neutral;
  1212.       board[rt] = rook; color[rt] = side; Pindex[rt] = Pindex[rf];
  1213.       board[rf] = no_piece; color[rf] = neutral;
  1214.       PieceList[side][Pindex[kt]] = kt;
  1215.       PieceList[side][Pindex[rt]] = rt;
  1216.       if (hashflag)
  1217.         {
  1218.           UpdateHashbd(side,king,kf,kt);
  1219.           UpdateHashbd(side,rook,rf,rt);
  1220.         }
  1221.     }
  1222.   return(true);
  1223. }
  1224.  
  1225.  
  1226. EnPassant(xside,f,t,iop)
  1227. short xside,f,t,iop;
  1228.  
  1229. /*
  1230.    Make or unmake an en passant move.
  1231. */
  1232.  
  1233. {
  1234. short l;
  1235.   if (t > f) l = t-8; else l = t+8;
  1236.   if (iop == 1)
  1237.     {
  1238.       board[l] = no_piece; color[l] = neutral;
  1239.     }
  1240.   else 
  1241.     {
  1242.       board[l] = pawn; color[l] = xside;
  1243.     }
  1244.   InitializeStats();
  1245. }
  1246.  
  1247.  
  1248. MakeMove(side,node,tempb,tempc,tempsf,tempst)
  1249. short side,*tempc,*tempb,*tempsf,*tempst;
  1250. struct leaf *node;
  1251.  
  1252. /*
  1253.    Update Arrays board[], color[], and Pindex[] to reflect the new board 
  1254.    position obtained after making the move pointed to by node. Also 
  1255.    update miscellaneous stuff that changes when a move is made. 
  1256. */
  1257.     
  1258. {
  1259. register short f,t;
  1260. short xside,ct,cf;
  1261.  
  1262.   xside = otherside[side];
  1263.   f = node->f; t = node->t; epsquare = -1;
  1264.   FROMsquare = f; TOsquare = t;
  1265.   INCscore = 0;
  1266.   GameList[++GameCnt].gmove = (f<<8) + t;
  1267.   if (node->flags & cstlmask)
  1268.     {
  1269.       GameList[GameCnt].piece = no_piece;
  1270.       GameList[GameCnt].color = side;
  1271.       castle(side,f,t,1);
  1272.     }
  1273.   else
  1274.     {
  1275.       *tempc = color[t]; *tempb = board[t];
  1276.       *tempsf = svalue[f]; *tempst = svalue[t];
  1277.       GameList[GameCnt].piece = *tempb;
  1278.       GameList[GameCnt].color = *tempc;
  1279.       if (*tempc != neutral)
  1280.         {
  1281.           UpdatePieceList(*tempc,t,1);
  1282.           if (*tempb == pawn) --PawnCnt[*tempc][column[t]];
  1283.           if (board[f] == pawn)
  1284.             {
  1285.               --PawnCnt[side][column[f]];
  1286.               ++PawnCnt[side][column[t]];
  1287.               cf = column[f]; ct = column[t];
  1288.               if (PawnCnt[side][ct] > 1+PawnCnt[side][cf])
  1289.                 INCscore -= 15;
  1290.               else if (PawnCnt[side][ct] < 1+PawnCnt[side][cf])
  1291.                 INCscore += 15;
  1292.               else if (ct == 0 || ct == 7 || PawnCnt[side][ct+ct-cf] == 0)
  1293.                 INCscore -= 15;
  1294.             }
  1295.           mtl[xside] -= value[*tempb];
  1296.           if (*tempb == pawn) pmtl[xside] -= valueP;
  1297.           if (hashflag) UpdateHashbd(xside,*tempb,-1,t);
  1298.           INCscore += *tempst;
  1299.         }
  1300.       color[t] = color[f]; board[t] = board[f]; svalue[t] = svalue[f];
  1301.       Pindex[t] = Pindex[f]; PieceList[side][Pindex[t]] = t;
  1302.       color[f] = neutral; board[f] = no_piece;
  1303.       if (board[t] == pawn)
  1304.         if (t-f == 16) epsquare = f+8;
  1305.         else if (f-t == 16) epsquare = f-8;
  1306.       if (node->flags & promote)
  1307.         {
  1308.           board[t] = queen;
  1309.           --PawnCnt[side][column[t]];
  1310.           mtl[side] += valueQ - valueP;
  1311.           pmtl[side] -= valueP;
  1312.           HasQueen[side] = true;
  1313.           if (hashflag)
  1314.             {
  1315.               UpdateHashbd(side,pawn,f,-1);
  1316.               UpdateHashbd(side,queen,f,-1);
  1317.             }
  1318.           INCscore -= *tempsf;
  1319.         } 
  1320.       if (board[t] == king) ++kingmoved[side];
  1321.       if (node->flags & epmask) EnPassant(xside,f,t,1);
  1322.       else if (hashflag) UpdateHashbd(side,board[t],f,t);
  1323.     }
  1324. }
  1325.  
  1326.  
  1327. UnmakeMove(side,node,tempb,tempc,tempsf,tempst)
  1328. short side,*tempc,*tempb,*tempsf,*tempst;
  1329. struct leaf *node;
  1330.  
  1331. /*
  1332.    Take back a move.
  1333. */
  1334.  
  1335. {
  1336. register short f,t;
  1337. short xside;
  1338.  
  1339.   xside = otherside[side];
  1340.   f = node->f; t = node->t; epsquare = -1;
  1341.   GameCnt--;
  1342.   if (node->flags & cstlmask) castle(side,f,t,2);
  1343.   else
  1344.     {
  1345.       color[f] = color[t]; board[f] = board[t]; svalue[f] = *tempsf;
  1346.       Pindex[f] = Pindex[t]; PieceList[side][Pindex[f]] = f;
  1347.       color[t] = *tempc; board[t] = *tempb; svalue[t] = *tempst;
  1348.       if (node->flags & promote)
  1349.         {
  1350.           board[f] = pawn;
  1351.           ++PawnCnt[side][column[t]];
  1352.           mtl[side] += valueP - valueQ;
  1353.           pmtl[side] += valueP;
  1354.           if (hashflag)
  1355.             {
  1356.               UpdateHashbd(side,queen,-1,t);
  1357.               UpdateHashbd(side,pawn,-1,t);
  1358.             }
  1359.         } 
  1360.       if (*tempc != neutral)
  1361.         {
  1362.           UpdatePieceList(*tempc,t,2);
  1363.           if (*tempb == pawn) ++PawnCnt[*tempc][column[t]];
  1364.           if (board[f] == pawn)
  1365.             {
  1366.               --PawnCnt[side][column[t]];
  1367.               ++PawnCnt[side][column[f]];
  1368.             }
  1369.           mtl[xside] += value[*tempb];
  1370.           if (*tempb == pawn) pmtl[xside] += valueP;
  1371.           if (hashflag) UpdateHashbd(xside,*tempb,-1,t);
  1372.         }
  1373.       if (board[f] == king) --kingmoved[side];
  1374.       if (node->flags & epmask) EnPassant(xside,f,t,2);
  1375.       else if (hashflag) UpdateHashbd(side,board[f],f,t);
  1376.     }
  1377. }
  1378.  
  1379.  
  1380. UpdateHashbd(side,piece,f,t)
  1381. short side,piece,f,t;
  1382.  
  1383. /*
  1384.    hashbd contains a 32 bit "signature" of the board position. hashkey 
  1385.    contains a 16 bit code used to address the hash table. When a move is 
  1386.    made, XOR'ing the hashcode of moved piece on the from and to squares 
  1387.    with the hashbd and hashkey values keeps things current. 
  1388. */
  1389.  
  1390. {
  1391.   if (f >= 0)
  1392.     {
  1393.       hashbd ^= hashcode[side][piece][f].bd;
  1394.       hashkey ^= hashcode[side][piece][f].key;
  1395.     }
  1396.   if (t >= 0)
  1397.     {
  1398.       hashbd ^= hashcode[side][piece][t].bd;
  1399.       hashkey ^= hashcode[side][piece][t].key;
  1400.     }
  1401. }
  1402.  
  1403.  
  1404. UpdatePieceList(side,sq,iop)
  1405. short side,sq,iop;
  1406.  
  1407. /*
  1408.    Update the PieceList and Pindex arrays when a piece is captured or 
  1409.    when a capture is unmade. 
  1410. */
  1411.  
  1412. {
  1413. register short i;
  1414.   if (iop == 1)
  1415.     {
  1416.       PieceCnt[side]--;
  1417.       for (i = Pindex[sq]; i <= PieceCnt[side]; i++)
  1418.         {
  1419.           PieceList[side][i] = PieceList[side][i+1];
  1420.           Pindex[PieceList[side][i]] = i;
  1421.         }
  1422.     }
  1423.   else
  1424.     {
  1425.       PieceCnt[side]++;
  1426.       PieceList[side][PieceCnt[side]] = sq;
  1427.       Pindex[sq] = PieceCnt[side];
  1428.     }
  1429. }
  1430.  
  1431.  
  1432. InitializeStats()
  1433.  
  1434. /*
  1435.    Scan thru the board seeing what's on each square. If a piece is found, 
  1436.    update the variables PieceCnt, PawnCnt, Pindex and PieceList. Also 
  1437.    determine the material for each side and set the hashkey and hashbd 
  1438.    variables to represent the current board position. Array 
  1439.    PieceList[side][indx] contains the location of all the pieces of 
  1440.    either side. Array Pindex[sq] contains the indx into PieceList for a 
  1441.    given square. 
  1442. */
  1443.  
  1444. {
  1445. register short i,sq;
  1446.   epsquare = -1;
  1447.   for (i = 0; i < 8; i++)
  1448.     PawnCnt[white][i] = PawnCnt[black][i] = 0;
  1449.   mtl[white] = mtl[black] = pmtl[white] = pmtl[black] = 0;
  1450.   PieceCnt[white] = PieceCnt[black] = 0;
  1451.   hashbd = hashkey = 0;
  1452.   for (sq = 0; sq < 64; sq++)
  1453.     if (color[sq] != neutral)
  1454.       {
  1455.         mtl[color[sq]] += value[board[sq]];
  1456.         if (board[sq] == pawn)
  1457.           {
  1458.             pmtl[color[sq]] += valueP;
  1459.             ++PawnCnt[color[sq]][column[sq]];
  1460.           }
  1461.         if (board[sq] == king) Pindex[sq] = 0;
  1462.           else Pindex[sq] = ++PieceCnt[color[sq]];
  1463.         PieceList[color[sq]][Pindex[sq]] = sq;
  1464.         hashbd ^= hashcode[color[sq]][board[sq]][sq].bd;
  1465.         hashkey ^= hashcode[color[sq]][board[sq]][sq].key;
  1466.       }
  1467. }
  1468.  
  1469.  
  1470. pick(p1,p2)
  1471. short p1,p2;
  1472.  
  1473. /*  
  1474.    Find the best move in the tree between indexes p1 and p2. Swap the 
  1475.    best move into the p1 element. 
  1476. */
  1477.  
  1478. {
  1479. register short p,s;
  1480. short p0,s0;
  1481. struct leaf temp;
  1482.  
  1483.   s0 = Tree[p1].score; p0 = p1;
  1484.   for (p = p1+1; p <= p2; p++)
  1485.     if ((s = Tree[p].score) > s0)
  1486.       {
  1487.         s0 = s; p0 = p;
  1488.       }
  1489.   if (p0 != p1)
  1490.     {
  1491.       temp = Tree[p1]; Tree[p1] = Tree[p0]; Tree[p0] = temp;
  1492.     }
  1493. }
  1494.  
  1495.  
  1496. repetition(cnt)
  1497. short *cnt;
  1498.  
  1499. /*
  1500.     Check for draw by threefold repetition.
  1501. */
  1502.  
  1503. {
  1504. register short i,c;
  1505. short f,t,b[64];
  1506. unsigned short m;
  1507.   *cnt = c = 0;
  1508.   if (GameCnt > Game50+3)
  1509.     {
  1510. /*
  1511.       memset((char *)b,0,64*sizeof(short));
  1512. */
  1513.       for (i = 0; i < 64; b[i++] = 0);
  1514.       for (i = GameCnt; i > Game50; i--)
  1515.         {
  1516.           m = GameList[i].gmove; f = m>>8; t = m & 0xFF;
  1517.           if (++b[f] == 0) c--; else c++;
  1518.           if (--b[t] == 0) c--; else c++;
  1519.           if (c == 0) (*cnt)++;
  1520.         }
  1521.     }
  1522. }
  1523.  
  1524.  
  1525. int SqAtakd(sq,side)
  1526. short sq,side;
  1527.  
  1528. /*
  1529.   See if any piece with color 'side' ataks sq.  First check for pawns
  1530.   or king, then try other pieces. Array Dcode is used to check for
  1531.   knight attacks or R,B,Q co-linearity.  
  1532. */
  1533.  
  1534. {
  1535. register short m,d;
  1536. short i,m0,m1,loc,piece,*PL;
  1537.  
  1538.   m1 = map[sq];
  1539.   if (side == white) m = m1-0x0F; else m = m1+0x0F;
  1540.   if (!(m & 0x88))
  1541.     if (board[unmap[m]] == pawn && color[unmap[m]] == side) return(true);
  1542.   if (side == white) m = m1-0x11; else m = m1+0x11;
  1543.   if (!(m & 0x88))
  1544.     if (board[unmap[m]] == pawn && color[unmap[m]] == side) return(true);
  1545.   if (distance(sq,PieceList[side][0]) == 1) return(true);
  1546.   
  1547.   PL = PieceList[side];
  1548.   for (i = 1; i <= PieceCnt[side]; i++)
  1549.     {
  1550.       loc = PL[i]; piece = board[loc];
  1551.       if (piece == pawn) continue;
  1552.       m0 = map[loc]; d = Dcode[abs(m1-m0)];
  1553.       if (d == 0 || (Pdir[d] & pbit[piece]) == 0) continue;
  1554.       if (piece == knight) return(true);
  1555.       else
  1556.         {
  1557.           if (m1 < m0) d = -d;
  1558.           for (m = m0+d; m != m1; m += d)
  1559.             if (color[unmap[m]] != neutral) break;
  1560.           if (m == m1) return(true);
  1561.         }
  1562.     }
  1563.   return(false);
  1564. }
  1565.  
  1566.  
  1567. ataks(side,a)
  1568. short side,*a;
  1569.  
  1570. /*
  1571.     Fill array atak[][] with info about ataks to a square.  Bits 8-15
  1572.     are set if the piece (king..pawn) ataks the square. Bits 0-7
  1573.     contain a count of total ataks to the square.
  1574. */
  1575.  
  1576. {
  1577. register short u,m;
  1578. short d,c,j,j1,j2,piece,i,m0,sq,*PL;
  1579.  
  1580. /*
  1581.   memset((char *)a,0,64*sizeof(short));
  1582. */
  1583.   for (u = 0; u < 64; a[u++] = 0);
  1584.   Dstart[pawn] = Dpwn[side]; Dstop[pawn] = Dstart[pawn] + 1;
  1585.   PL = PieceList[side];
  1586.   for (i = 0; i <= PieceCnt[side]; i++)
  1587.     {
  1588.       sq = PL[i];
  1589.       m0 = map[sq];
  1590.       piece = board[sq];
  1591.       c = control[piece]; j1 = Dstart[piece]; j2 = Dstop[piece];
  1592.       if (sweep[piece])
  1593.         for (j = j1; j <= j2; j++)
  1594.           {
  1595.             d = Dir[j]; m = m0+d;
  1596.             while (!(m & 0x88))
  1597.               {
  1598.                 u = unmap[m];
  1599.                 a[u] = ++a[u] | c;
  1600.                 if (color[u] == neutral) m += d;
  1601.                 else break;
  1602.               }
  1603.           }
  1604.       else
  1605.         for (j = j1; j <= j2; j++)
  1606.           if (!((m = m0+Dir[j]) & 0x88))
  1607.             {
  1608.               u = unmap[m];
  1609.               a[u] = ++a[u] | c;
  1610.             }
  1611.     }
  1612. }
  1613.  
  1614.  
  1615. /* ............    POSITIONAL EVALUATION ROUTINES    ............ */
  1616.  
  1617. ScorePosition(side,score)
  1618. short side,*score;
  1619.  
  1620. /*
  1621.    Perform normal static evaluation of board position. A score is 
  1622.    generated for each piece and these are summed to get a score for each 
  1623.    side. 
  1624. */
  1625.  
  1626. {
  1627. register short sq,s;
  1628. short i,xside,pscore[3];
  1629.  
  1630.   wking = PieceList[white][0]; bking = PieceList[black][0];
  1631.   UpdateWeights();
  1632.   xside = otherside[side];
  1633.   pscore[white] = pscore[black] = 0;
  1634.  
  1635.   for (c1 = white; c1 <= black; c1++)
  1636.     {
  1637.       c2 = otherside[c1];
  1638.       if (c1 == white) EnemyKing = bking; else EnemyKing = wking;
  1639.       atk1 = atak[c1]; atk2 = atak[c2];
  1640.       PC1 = PawnCnt[c1]; PC2 = PawnCnt[c2];
  1641.       for (i = 0; i <= PieceCnt[c1]; i++)
  1642.         {
  1643.           sq = PieceList[c1][i];
  1644.           s = SqValue(sq,side);
  1645.           pscore[c1] += s;
  1646.           svalue[sq] = s;
  1647.         }
  1648.     }
  1649.   if (hung[side] > 1) pscore[side] += HUNGX;
  1650.   if (hung[xside] > 1) pscore[xside] += HUNGX;
  1651.   
  1652.   *score = mtl[side] - mtl[xside] + pscore[side] - pscore[xside] + 10;
  1653.   if (dither) *score += rand() % dither;
  1654.   
  1655.   if (*score > 0 && pmtl[side] == 0)
  1656.     if (emtl[side] < valueR) *score = 0;
  1657.     else if (*score < valueR) *score /= 2;
  1658.   if (*score < 0 && pmtl[xside] == 0)
  1659.     if (emtl[xside] < valueR) *score = 0;
  1660.     else if (-*score < valueR) *score /= 2;
  1661.     
  1662.   if (mtl[xside] == valueK && emtl[side] > valueB) *score += 200;
  1663.   if (mtl[side] == valueK && emtl[xside] > valueB) *score -= 200;
  1664. }
  1665.  
  1666.  
  1667. ScoreLoneKing(side,score)
  1668. short side,*score;
  1669.  
  1670. /* 
  1671.    Static evaluation when loser has only a king and winner has no pawns
  1672.    or no pieces.
  1673. */
  1674.  
  1675. {
  1676. short winner,loser,king1,king2,s,i;
  1677.  
  1678.   UpdateWeights();
  1679.   if (mtl[white] > mtl[black]) winner = white; else winner = black;
  1680.   loser = otherside[winner];
  1681.   king1 = PieceList[winner][0]; king2 = PieceList[loser][0];
  1682.   
  1683.   s = 0;
  1684.   
  1685.   if (pmtl[winner] > 0)
  1686.     for (i = 1; i <= PieceCnt[winner]; i++)
  1687.       s += ScoreKPK(side,winner,loser,king1,king2,PieceList[winner][i]);
  1688.       
  1689.   else if (emtl[winner] == valueB+valueN)
  1690.     s = ScoreKBNK(winner,king1,king2);
  1691.     
  1692.   else if (emtl[winner] > valueB)
  1693.     s = 500 + emtl[winner] - DyingKing[king2] - 2*distance(king1,king2);
  1694.     
  1695.   if (side == winner) *score = s; else *score = -s;
  1696. }
  1697.  
  1698.  
  1699. int ScoreKPK(side,winner,loser,king1,king2,sq)
  1700. short side,winner,loser,king1,king2,sq;
  1701.  
  1702. /*
  1703.    Score King and Pawns versus King endings.
  1704. */
  1705.  
  1706. {
  1707. short s,r;
  1708.   
  1709.   if (PieceCnt[winner] == 1) s = 50; else s = 120;
  1710.   if (winner == white)
  1711.     {
  1712.       if (side == loser) r = row[sq]-1; else r = row[sq];
  1713.       if (row[king2] >= r && distance(sq,king2) < 8-r) s += 10*row[sq];
  1714.       else s = 500+50*row[sq];
  1715.       if (row[sq] < 6) sq += 16; else sq += 8;
  1716.     }
  1717.   else
  1718.     {
  1719.       if (side == loser) r = row[sq]+1; else r = row[sq];
  1720.       if (row[king2] <= r && distance(sq,king2) < r+1) s += 10*(7-row[sq]);
  1721.       else s = 500+50*(7-row[sq]);
  1722.       if (row[sq] > 1) sq -= 16; else sq -= 8;
  1723.     }
  1724.   s += 8*(taxicab(king2,sq) - taxicab(king1,sq));
  1725.   return(s);
  1726. }
  1727.  
  1728.  
  1729. int ScoreKBNK(winner,king1,king2)
  1730. short winner,king1,king2;
  1731.  
  1732. /*
  1733.    Score King+Bishop+Knight versus King endings.
  1734.    This doesn't work all that well but it's better than nothing.
  1735. */
  1736.  
  1737. {
  1738. short s;
  1739.   s = emtl[winner] - 300;
  1740.   if (KBNKsq == 0) s += KBNK[king2];
  1741.   else s += KBNK[locn[row[king2]][7-column[king2]]];
  1742.   s -= taxicab(king1,king2);
  1743.   s -= distance(PieceList[winner][1],king2);
  1744.   s -= distance(PieceList[winner][2],king2);
  1745.   return(s);
  1746. }
  1747.  
  1748.  
  1749. SqValue(sq,side)
  1750. short sq,side;
  1751.  
  1752. /*
  1753.    Calculate the positional value for the piece on 'sq'.
  1754. */
  1755.  
  1756. {
  1757. register short j,fyle,rank;
  1758. short s,piece,a1,a2,in_square,r,mob,e,c;
  1759.  
  1760.   piece = board[sq];
  1761.   a1 = (atk1[sq] & 0x4FFF); a2 = (atk2[sq] & 0x4FFF);
  1762.   rank = row[sq]; fyle = column[sq];
  1763.   s = 0;
  1764.   if (piece == pawn && c1 == white)
  1765.     {
  1766.       s = Mwpawn[sq];
  1767.       if (sq == 11 || sq == 12)
  1768.         if (color[sq+8] != neutral) s += PEDRNK2B;
  1769.       if ((fyle == 0 || PC1[fyle-1] == 0) &&
  1770.           (fyle == 7 || PC1[fyle+1] == 0))
  1771.         s += ISOLANI[fyle];
  1772.       else if (PC1[fyle] > 1) s += PDOUBLED;
  1773.       if (a1 < ctlP && atk1[sq+8] < ctlP)
  1774.         {
  1775.           s += BACKWARD[a2 & 0xFF];
  1776.           if (PC2[fyle] == 0) s += PWEAKH;
  1777.           if (color[sq+8] != neutral) s += PBLOK;
  1778.         }
  1779.       if (PC2[fyle] == 0)
  1780.         {
  1781.           if (side == black) r = rank-1; else r = rank;
  1782.           in_square = (row[bking] >= r && distance(sq,bking) < 8-r);
  1783.           if (a2 == 0 || side == white) e = 0; else e = 1;
  1784.           for (j = sq+8; j < 64; j += 8)
  1785.             if (atk2[j] >= ctlP) { e = 2; break; }
  1786.             else if (atk2[j] > 0 || color[j] != neutral) e = 1;
  1787.           if (e == 2) s += (stage*PassedPawn3[rank]) / 10;
  1788.           else if (in_square || e == 1) s += (stage*PassedPawn2[rank]) / 10;
  1789.           else if (emtl[black] > 0) s += (stage*PassedPawn1[rank]) / 10;
  1790.           else s += PassedPawn0[rank];
  1791.         }
  1792.     }
  1793.   else if (piece == pawn && c1 == black)
  1794.     {
  1795.       s = Mbpawn[sq];
  1796.       if (sq == 51 || sq == 52)
  1797.         if (color[sq-8] != neutral) s += PEDRNK2B;
  1798.       if ((fyle == 0 || PC1[fyle-1] == 0) &&
  1799.           (fyle == 7 || PC1[fyle+1] == 0))
  1800.         s += ISOLANI[fyle];
  1801.       else if (PC1[fyle] > 1) s += PDOUBLED;
  1802.       if (a1 < ctlP && atk1[sq-8] < ctlP)
  1803.         {
  1804.           s += BACKWARD[a2 & 0xFF];
  1805.           if (PC2[fyle] == 0) s += PWEAKH;
  1806.           if (color[sq-8] != neutral) s += PBLOK;
  1807.         }
  1808.       if (PC2[fyle] == 0)
  1809.         {
  1810.           if (side == white) r = rank+1; else r = rank;
  1811.           in_square = (row[wking] <= r && distance(sq,wking) < r+1);
  1812.           if (a2 == 0 || side == black) e = 0; else e = 1;
  1813.           for (j = sq-8; j >= 0; j -= 8)
  1814.             if (atk2[j] >= ctlP) { e = 2; break; }
  1815.             else if (atk2[j] > 0 || color[j] != neutral) e = 1;
  1816.           if (e == 2) s += (stage*PassedPawn3[7-rank]) / 10;
  1817.           else if (in_square || e == 1) s += (stage*PassedPawn2[7-rank]) / 10;
  1818.           else if (emtl[white] > 0) s += (stage*PassedPawn1[7-rank]) / 10;
  1819.           else s += PassedPawn0[7-rank];
  1820.         }
  1821.     }
  1822.   else if (piece == knight)
  1823.     {
  1824.       s = Mknight[c1][sq];
  1825.     }
  1826.   else if (piece == bishop)
  1827.     {
  1828.       s = Mbishop[c1][sq];
  1829.       BRscan(sq,&s,&mob);
  1830.       s += BMBLTY[mob];
  1831.     }
  1832.   else if (piece == rook)
  1833.     {
  1834.       s += RookBonus;
  1835.       BRscan(sq,&s,&mob);
  1836.       s += RMBLTY[mob];
  1837.       if (PC1[fyle] == 0) s += RHOPN;
  1838.       if (PC2[fyle] == 0) s += RHOPNX;
  1839.       if (rank == rank7[c1] && pmtl[c2] > 100) s += 10;
  1840.       if (stage > 2) s += 14 - taxicab(sq,EnemyKing);
  1841.     }
  1842.   else if (piece == queen)
  1843.     {
  1844.       if (stage > 2) s += 14 - taxicab(sq,EnemyKing);
  1845.       if (distance(sq,EnemyKing) < 3) s += 12;
  1846.     }
  1847.   else if (piece == king)
  1848.     {
  1849.       s = Mking[c1][sq];
  1850.       if (KSFTY > 0)
  1851.         if (Developed[c2] || stage > 0) KingScan(sq,&s);
  1852.       if (castld[c1]) s += KCASTLD;
  1853.       else if (kingmoved[c1]) s += KMOVD;
  1854.  
  1855.       if (PC1[fyle] == 0) s += KHOPN;
  1856.       if (PC2[fyle] == 0) s += KHOPNX;
  1857.       if (fyle == 1 || fyle == 2 || fyle == 3 || fyle == 7)
  1858.         {
  1859.           if (PC1[fyle-1] == 0) s += KHOPN;
  1860.           if (PC2[fyle-1] == 0) s += KHOPNX;
  1861.         }
  1862.       if (fyle == 4 || fyle == 5 || fyle == 6 || fyle == 0)
  1863.         {
  1864.           if (PC1[fyle+1] == 0) s += KHOPN;
  1865.           if (PC2[fyle+1] == 0) s += KHOPNX;
  1866.         }
  1867.       if (fyle == 2)
  1868.         {
  1869.           if (PC1[0] == 0) s += KHOPN;
  1870.           if (PC2[0] == 0) s += KHOPNX;
  1871.         }
  1872.       if (fyle == 5)
  1873.         {
  1874.           if (PC1[7] == 0) s += KHOPN;
  1875.           if (PC2[7] == 0) s += KHOPNX;
  1876.         }
  1877.     }
  1878.     
  1879.   if (a2 > 0) 
  1880.     {
  1881.       c = (control[piece] & 0x4FFF);
  1882.       if (a1 == 0 || a2 > c+1)
  1883.         {
  1884.           s += HUNGP;
  1885.           ++hung[c1];
  1886.           if (piece != king && trapped(sq,piece)) ++hung[c1];
  1887.         }
  1888.       else if (piece != pawn || a2 > a1)
  1889.         if (a2 >= c || a1 < ctlP) s += ATAKD;
  1890.     }
  1891.   return(s);
  1892. }
  1893.  
  1894.  
  1895. KingScan(sq,s)
  1896. short sq,*s;
  1897.  
  1898. /*
  1899.    Assign penalties if king can be threatened by checks, if squares
  1900.    near the king are controlled by the enemy (especially the queen),
  1901.    or if there are no pawns near the king.
  1902. */
  1903.  
  1904. #define ScoreThreat\
  1905.   if (color[u] != c2)\
  1906.     if (atk1[u] == 0 || (atk2[u] & 0xFF) > 1) ++cnt;\
  1907.     else *s -= 3
  1908.  
  1909. {
  1910. register short m,u;
  1911. short d,i,m0,cnt,ok;
  1912.  
  1913.   cnt = 0;
  1914.   m0 = map[sq];
  1915.   if (HasBishop[c2] || HasQueen[c2])
  1916.     for (i = Dstart[bishop]; i <= Dstop[bishop]; i++)
  1917.       {
  1918.         d = Dir[i]; m = m0+d;
  1919.         while (!(m & 0x88))
  1920.           {
  1921.             u = unmap[m];
  1922.             if (atk2[u] & ctlBQ) ScoreThreat;
  1923.             if (color[u] != neutral) break;
  1924.             m += d;
  1925.           }
  1926.       }
  1927.   if (HasRook[c2] || HasQueen[c2])
  1928.     for (i = Dstart[rook]; i <= Dstop[rook]; i++)
  1929.       {
  1930.         d = Dir[i]; m = m0+d;
  1931.         while (!(m & 0x88))
  1932.           {
  1933.             u = unmap[m];
  1934.             if (atk2[u] & ctlRQ) ScoreThreat;
  1935.             if (color[u] != neutral) break;
  1936.             m += d;
  1937.           }
  1938.       }
  1939.   if (HasKnight[c2])
  1940.     for (i = Dstart[knight]; i <= Dstop[knight]; i++)
  1941.       if (!((m = m0+Dir[i]) & 0x88))
  1942.         {
  1943.           u = unmap[m];
  1944.           if (atk2[u] & ctlNN) ScoreThreat;
  1945.         }
  1946.   *s += (KSFTY*Kthreat[cnt]) / 16;
  1947.  
  1948.   cnt = 0; ok = false;
  1949.   m0 = map[sq];
  1950.   for (i = Dstart[king]; i <= Dstop[king]; i++)
  1951.     if (!((m = m0+Dir[i]) & 0x88))
  1952.       {
  1953.         u = unmap[m];
  1954.         if (board[u] == pawn) ok = true;
  1955.         if (atk2[u] > atk1[u])
  1956.           {
  1957.             ++cnt;
  1958.             if (atk2[u] & ctlQ)
  1959.               if (atk2[u] > ctlQ+1 && atk1[u] < ctlQ) *s -= 4*KSFTY;
  1960.           }
  1961.       }
  1962.   if (!ok) *s -= KSFTY;
  1963.   if (cnt > 1) *s -= KSFTY;
  1964. }
  1965.  
  1966.  
  1967. BRscan(sq,s,mob)
  1968. short sq,*s,*mob;
  1969.  
  1970. /*
  1971.    Find Bishop and Rook mobility, XRAY attacks, and pins. Increment the 
  1972.    hung[] array if a pin is found. 
  1973. */
  1974.  
  1975. {
  1976. register short m,u;
  1977. short d,j,m0,piece,pin,*Kf;
  1978.  
  1979.   Kf = Kfield[c1];
  1980.   *mob = 0;
  1981.   m0 = map[sq]; piece = board[sq];
  1982.   for (j = Dstart[piece]; j <= Dstop[piece]; j++)
  1983.     {
  1984.       pin = -1;
  1985.       d = Dir[j]; m = m0+d;
  1986.       while (!(m & 0x88))
  1987.         {
  1988.           u = unmap[m]; *s += Kf[u];
  1989.           if (color[u] == neutral)
  1990.             {
  1991.               (*mob)++;
  1992.               m += d;
  1993.             }
  1994.           else if (pin < 0)
  1995.             {
  1996.               if (board[u] == pawn || board[u] == king) break;
  1997.               pin = u;
  1998.               m += d;
  1999.             }
  2000.           else if (color[u] == c2 && (board[u] > piece || atk2[u] == 0))
  2001.             {
  2002.               if (color[pin] == c2)
  2003.                 {
  2004.                   *s += PINVAL;
  2005.                   if (atk2[pin] == 0 ||
  2006.                       atk1[pin] > control[board[pin]]+1)
  2007.                     ++hung[c2];
  2008.                 }
  2009.               else *s += XRAY;
  2010.               break;
  2011.             }
  2012.           else break;
  2013.         }
  2014.     }
  2015. }
  2016.  
  2017.  
  2018. int trapped(sq,piece)
  2019. short sq,piece;
  2020.  
  2021. /*
  2022.    See if the attacked piece has unattacked squares to move to.
  2023. */
  2024.  
  2025. {
  2026. register short u,m,d;
  2027. short i,m0;
  2028.  
  2029.   m0 = map[sq];
  2030.   if (sweep[piece])
  2031.     for (i = Dstart[piece]; i <= Dstop[piece]; i++)
  2032.       {
  2033.         d = Dir[i]; m = m0+d;
  2034.         while (!(m & 0x88))
  2035.           {
  2036.             u = unmap[m];
  2037.             if (color[u] == c1) break;
  2038.             if (atk2[u] == 0 || board[u] >= piece) return(false);
  2039.             if (color[u] == c2) break;
  2040.             m += d;
  2041.           }
  2042.       }
  2043.   else if (piece == pawn)
  2044.     {
  2045.       if (c1 == white) u = sq+8; else u = sq-8;
  2046.       if (color[u] == neutral && atk1[u] >= atk2[u])
  2047.         return(false);
  2048.       if (!((m = m0+Dir[Dpwn[c1]]) & 0x88))
  2049.         if (color[unmap[m]] == c2) return(false);
  2050.       if (!((m = m0+Dir[Dpwn[c1]+1]) & 0x88))
  2051.         if (color[unmap[m]] == c2) return(false);
  2052.     }
  2053.   else
  2054.     {
  2055.       for (i = Dstart[piece]; i <= Dstop[piece]; i++)
  2056.         if (!((m = m0+Dir[i]) & 0x88))
  2057.           {
  2058.             u = unmap[m];
  2059.             if (color[u] != c1)
  2060.               if (atk2[u] == 0 || board[u] >= piece) return(false);
  2061.           }
  2062.     }
  2063.   return(true);
  2064. }
  2065.  
  2066.  
  2067. ExaminePosition()
  2068.  
  2069. /*
  2070.    This is done one time before the search is started. Set up arrays 
  2071.    Mwpawn, Mbpawn, Mknight, Mbishop, Mking which are used in the 
  2072.    SqValue() function to determine the positional value of each piece. 
  2073. */
  2074.  
  2075. {
  2076. register short i,sq;
  2077. short wpadv,bpadv,wstrong,bstrong,z,side,pp,j,val,Pd,fyle,rank;
  2078.  
  2079.   wking = PieceList[white][0]; bking = PieceList[black][0];
  2080.   ataks(white,atak[white]); ataks(black,atak[black]);
  2081.   Zwmtl = Zbmtl = 0;
  2082.   UpdateWeights();
  2083.   HasPawn[white] = HasPawn[black] = 0;
  2084.   HasKnight[white] = HasKnight[black] = 0;
  2085.   HasBishop[white] = HasBishop[black] = 0;
  2086.   HasRook[white] = HasRook[black] = 0;
  2087.   HasQueen[white] = HasQueen[black] = 0;
  2088.   for (side = white; side <= black; side++)
  2089.     for (i = 0; i <= PieceCnt[side]; i++)
  2090.       switch (board[PieceList[side][i]])
  2091.         {
  2092.           case pawn : ++HasPawn[side]; break;
  2093.           case knight : ++HasKnight[side]; break;
  2094.           case bishop : ++HasBishop[side]; break;
  2095.           case rook : ++HasRook[side]; break;
  2096.           case queen : ++HasQueen[side]; break;
  2097.         }
  2098.   if (!Developed[white])
  2099.     Developed[white] = (board[1] != knight && board[2] != bishop &&
  2100.                         board[5] != bishop && board[6] != knight);
  2101.   if (!Developed[black])
  2102.     Developed[black] = (board[57] != knight && board[58] != bishop &&
  2103.                         board[61] != bishop && board[62] != knight);
  2104.   if (!PawnStorm && stage < 5)
  2105.     PawnStorm = ((column[wking] < 3 && column[bking] > 4) ||
  2106.                  (column[wking] > 4 && column[bking] < 3));
  2107.   
  2108.   CopyBoard(pknight,Mknight[white]);
  2109.   CopyBoard(pknight,Mknight[black]);
  2110.   CopyBoard(pbishop,Mbishop[white]);
  2111.   CopyBoard(pbishop,Mbishop[black]);
  2112.   BlendBoard(KingOpening,KingEnding,Mking[white]);
  2113.   BlendBoard(KingOpening,KingEnding,Mking[black]);
  2114.   
  2115.   for (sq = 0; sq < 64; sq++)
  2116.     {
  2117.       fyle = column[sq]; rank = row[sq];
  2118.       wstrong = bstrong = true;
  2119.       for (i = sq; i < 64; i += 8)
  2120.         if (atak[black][i] >= ctlP) wstrong = false;
  2121.       for (i = sq; i >= 0; i -= 8)
  2122.         if (atak[white][i] >= ctlP) bstrong = false;
  2123.       wpadv = bpadv = PADVNCM;
  2124.       if ((fyle == 0 || PawnCnt[white][fyle-1] == 0) &&
  2125.           (fyle == 7 || PawnCnt[white][fyle+1] == 0)) wpadv = PADVNCI;
  2126.       if ((fyle == 0 || PawnCnt[black][fyle-1] == 0) &&
  2127.           (fyle == 7 || PawnCnt[black][fyle+1] == 0)) bpadv = PADVNCI;
  2128.       Mwpawn[sq] = (wpadv*PawnAdvance[sq]) / 10;
  2129.       Mbpawn[sq] = (bpadv*PawnAdvance[63-sq]) / 10;
  2130.       Mwpawn[sq] += PawnBonus; Mbpawn[sq] += PawnBonus;
  2131.       if (castld[white] || kingmoved[white])
  2132.         {
  2133.           if ((fyle < 3 || fyle > 4) && distance(sq,wking) < 3)
  2134.             Mwpawn[sq] += PAWNSHIELD;
  2135.         }
  2136.       else if (rank < 3 && (fyle < 2 || fyle > 5))
  2137.         Mwpawn[sq] += PAWNSHIELD / 2;
  2138.       if (castld[black] || kingmoved[black])
  2139.         {
  2140.           if ((fyle < 3 || fyle > 4) && distance(sq,bking) < 3)
  2141.             Mbpawn[sq] += PAWNSHIELD;
  2142.         }
  2143.       else if (rank > 4 && (fyle < 2 || fyle > 5))
  2144.         Mbpawn[sq] += PAWNSHIELD / 2;
  2145.       if (PawnStorm)
  2146.         {
  2147.           if ((column[wking] < 4 && fyle > 4) ||
  2148.               (column[wking] > 3 && fyle < 3)) Mwpawn[sq] += 3*rank - 21;
  2149.           if ((column[bking] < 4 && fyle > 4) ||
  2150.               (column[bking] > 3 && fyle < 3)) Mbpawn[sq] -= 3*rank;
  2151.         }
  2152.         
  2153.       Mknight[white][sq] += 5 - distance(sq,bking);
  2154.       Mknight[white][sq] += 5 - distance(sq,wking);
  2155.       Mknight[black][sq] += 5 - distance(sq,wking);
  2156.       Mknight[black][sq] += 5 - distance(sq,bking);
  2157.       Mbishop[white][sq] += BishopBonus;
  2158.       Mbishop[black][sq] += BishopBonus;
  2159.       for (i = 0; i <= PieceCnt[black]; i++)
  2160.         if (distance(sq,PieceList[black][i]) < 3)
  2161.           Mknight[white][sq] += KNIGHTPOST;
  2162.       for (i = 0; i <= PieceCnt[white]; i++)
  2163.         if (distance(sq,PieceList[white][i]) < 3)
  2164.           Mknight[black][sq] += KNIGHTPOST;
  2165.       if (wstrong) Mknight[white][sq] += KNIGHTSTRONG;
  2166.       if (bstrong) Mknight[black][sq] += KNIGHTSTRONG;
  2167.       if (wstrong) Mbishop[white][sq] += BISHOPSTRONG;
  2168.       if (bstrong) Mbishop[black][sq] += BISHOPSTRONG;
  2169.       
  2170.       if (HasBishop[white] == 2) Mbishop[white][sq] += 8;
  2171.       if (HasBishop[black] == 2) Mbishop[black][sq] += 8;
  2172.       if (HasKnight[white] == 2) Mknight[white][sq] += 5;
  2173.       if (HasKnight[black] == 2) Mknight[black][sq] += 5;
  2174.       
  2175.       if (board[sq] == bishop)
  2176.         if (rank % 2 == fyle % 2) KBNKsq = 0; else KBNKsq = 7;
  2177.         
  2178.       Kfield[white][sq] = Kfield[black][sq] = 0;
  2179.       if (distance(sq,wking) == 1) Kfield[black][sq] = KATAK;
  2180.       if (distance(sq,bking) == 1) Kfield[white][sq] = KATAK;
  2181.       
  2182.       Pd = 0;
  2183.       for (i = 0; i < 64; i++)
  2184.         if (board[i] == pawn)
  2185.           {
  2186.             if (color[i] == white)
  2187.               {
  2188.                 pp = true;
  2189.                 if (row[i] == 6) z = i+8; else z = i+16;
  2190.                 for (j = i+8; j < 64; j += 8)
  2191.                   if (atak[black][j] > ctlP || board[j] == pawn) pp = false;
  2192.               }
  2193.             else
  2194.               {
  2195.                 pp = true;
  2196.                 if (row[i] == 1) z = i-8; else z = i-16;
  2197.                 for (j = i-8; j >= 0; j -= 8)
  2198.                   if (atak[white][j] > ctlP || board[j] == pawn) pp = false;
  2199.               }
  2200.             if (pp) Pd += 5*taxicab(sq,z); else Pd += taxicab(sq,z);
  2201.           }
  2202.       if (Pd != 0)
  2203.         {
  2204.           val = (Pd*stage2) / 10;
  2205.           Mking[white][sq] -= val;
  2206.           Mking[black][sq] -= val;
  2207.         }
  2208.     }
  2209. }
  2210.  
  2211.  
  2212. UpdateWeights()
  2213.  
  2214. /* 
  2215.    If material balance has changed, determine the values for the 
  2216.    positional evaluation terms. 
  2217. */
  2218.  
  2219. {
  2220. short tmtl;
  2221.  
  2222.   if (mtl[white] != Zwmtl || mtl[black] != Zbmtl)
  2223.     {
  2224.       Zwmtl = mtl[white]; Zbmtl = mtl[black];
  2225.       emtl[white] = Zwmtl - pmtl[white] - valueK;
  2226.       emtl[black] = Zbmtl - pmtl[black] - valueK;
  2227.       tmtl = emtl[white] + emtl[black];
  2228.       if (tmtl > 6600) stage = 0;
  2229.       else if (tmtl < 1400) stage = 10;
  2230.       else stage = (6600-tmtl) / 520;
  2231.       if (tmtl > 3600) stage2 = 0;
  2232.       else if (tmtl < 1400) stage2 = 10;
  2233.       else stage2 = (3600-tmtl) / 220;
  2234.       
  2235.       PEDRNK2B = -15;         /* centre pawn on 2nd rank & blocked */
  2236.       PBLOK = -4;             /* blocked backward pawn */
  2237.       PDOUBLED = -14;         /* doubled pawn */
  2238.       PWEAKH  = -4;           /* weak pawn on half open file */
  2239.       PAWNSHIELD = 10-stage;  /* pawn near friendly king */
  2240.       PADVNCM =  10;          /* advanced pawn multiplier */
  2241.       PADVNCI = 7;            /* muliplier for isolated pawn */
  2242.       PawnBonus = stage;
  2243.       
  2244.       KNIGHTPOST = (stage+2)/3;   /* knight near enemy pieces */
  2245.       KNIGHTSTRONG = (stage+6)/2; /* occupies pawn hole */
  2246.       
  2247.       BISHOPSTRONG = (stage+6)/2; /* occupies pawn hole */
  2248.       BishopBonus = 2*stage;
  2249.       
  2250.       RHOPN    = 10;          /* rook on half open file */
  2251.       RHOPNX   = 4;
  2252.       RookBonus = 6*stage;
  2253.       
  2254.       XRAY     = 8;           /* Xray attack on piece */
  2255.       PINVAL   = 10;          /* Pin */
  2256.       
  2257.       KHOPN    = (3*stage-30) / 2; /* king on half open file */
  2258.       KHOPNX   = KHOPN / 2;
  2259.       KCASTLD  = 10 - stage;
  2260.       KMOVD    = -40 / (stage+1);  /* king moved before castling */
  2261.       KATAK    = (10-stage) / 2;   /* B,R attacks near enemy king */
  2262.       if (stage < 8) KSFTY = 16-2*stage; else KSFTY = 0;
  2263.       
  2264.       ATAKD    = -6;          /* defender > attacker */
  2265.       HUNGP    = -8;          /* each hung piece */
  2266.       HUNGX    = -12;         /* extra for >1 hung piece */
  2267.     }
  2268. }
  2269.  
  2270.  
  2271. int distance(a,b)
  2272. short a,b;
  2273. {
  2274. register short d1,d2;
  2275.  
  2276.   d1 = abs(column[a]-column[b]);
  2277.   d2 = abs(row[a]-row[b]);
  2278.   if (d1 > d2) return(d1); else return(d2);
  2279. }
  2280.  
  2281.  
  2282. BlendBoard(a,b,c)
  2283. short a[64],b[64],c[64];
  2284. {
  2285. register int sq;
  2286.   for (sq = 0; sq < 64; sq++)
  2287.     c[sq] = (a[sq]*(10-stage) + b[sq]*stage) / 10;
  2288. }
  2289.  
  2290.  
  2291. CopyBoard(a,b)
  2292. short a[64],b[64];
  2293. {
  2294. register int sq;
  2295.   for (sq = 0; sq < 64; sq++)
  2296.     b[sq] = a[sq];
  2297. }
  2298.